package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessEvaluate;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.NewEvaluateVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.hedgehog.ratingbar.RatingBar;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/3/23 17:23
 * 修改备注：
 * ================================================
 */
public class TrainEvaluateActivity extends BaseActivity {


    @Bind(R.id.train_evaluate_rating_bar_expert)
    RatingBar trainEvaluateRatingBarExpert;
    @Bind(R.id.train_tv_train_expert)
    TextView trainTvTrainExpert;
    @Bind(R.id.train_evaluate_rating_bar_content)
    RatingBar trainEvaluateRatingBarContent;
    @Bind(R.id.train_tv_train_content)
    TextView trainTvTrainContent;
    @Bind(R.id.train_evaluate_rating_bar_sponsor)
    RatingBar trainEvaluateRatingBarSponsor;
    @Bind(R.id.train_tv_train_sponsor)
    TextView trainTvTrainSponsor;
    @Bind(R.id.train_evaluate_et_opinion)
    EditText trainEvaluateEtOpinion;
    @Bind(R.id.train_evaluate_btn_submit)
    Button trainEvaluateBtnSubmit;


    private String starExpert = "0";  // 专家评价分数
    private String starContent = "0"; // 内容评价分数
    private String starSponsor = "0"; // 主办方评价分数

    private String trainingId; // 培训ID

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_train_evaluate);
        setTitle("评价详情");
    }

    @Override
    public void initMember() {

        if (getIntent().hasExtra("JPush_evaluate")) {
            trainingId = (String) getIntent().getSerializableExtra("JPush_evaluate");
        } else {
            trainingId = (String) getIntent().getSerializableExtra("evaluateDetail");
            BusinessEvaluate.getEvaluateDetails(this, trainingId, mHandler);
            checkEvaluate();
            trainTvTrainExpert.setVisibility(View.INVISIBLE);
            trainTvTrainContent.setVisibility(View.INVISIBLE);
            trainTvTrainSponsor.setVisibility(View.INVISIBLE);
        }


    }

    /**
     * 已评价状态 - 查看评价
     */
    private void checkEvaluate() {

        trainEvaluateRatingBarExpert.setmClickable(false);
        trainEvaluateRatingBarContent.setmClickable(false);
        trainEvaluateRatingBarSponsor.setmClickable(false);

        // 设置editText不可编辑
        trainEvaluateEtOpinion.setFocusable(false);
        trainEvaluateEtOpinion.setEnabled(false);
        trainEvaluateEtOpinion.setFocusableInTouchMode(false);

        // 隐藏提交按钮
        trainEvaluateBtnSubmit.setVisibility(View.GONE);

    }

    /**
     * 展示评价星星个数
     *
     * @param iExpert  // 专家评价分数
     * @param iContent // 内容评价分数
     * @param iSponsor // 主办方评价分数
     */
    private void setStar(int iExpert, int iContent, int iSponsor) {

        trainEvaluateRatingBarExpert.setStar(iExpert);
        trainEvaluateRatingBarContent.setStar(iContent);
        trainEvaluateRatingBarSponsor.setStar(iSponsor);

        setStarText(trainTvTrainExpert, iExpert);
        setStarText(trainTvTrainContent, iContent);
        setStarText(trainTvTrainSponsor, iSponsor);

    }

    /**
     * 设置评价对应的文本
     *
     * @param textView star对应的text
     * @param grade    评分等级
     */
    private void setStarText(TextView textView, int grade) {

        switch (grade) {
            case 1:
                textView.setText("非常不满意");
                break;
            case 2:
                textView.setText("不满意");
                break;
            case 3:
                textView.setText("一般");
                break;
            case 4:
                textView.setText("满意");
                break;
            case 5:
                textView.setText("非常满意");
                break;
        }

    }

    @Override
    public void addListener() {

        // 培训专家打分
        trainEvaluateRatingBarExpert.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float RatingCount) {
                switch ((int) RatingCount) {
                    case 0:
                        trainTvTrainExpert.setText("请打分");
                        starExpert = "0";
                        break;
                    case 1:
                        trainTvTrainExpert.setText("非常不满意");
                        starExpert = "1";
                        break;
                    case 2:
                        trainTvTrainExpert.setText("不满意");
                        starExpert = "2";
                        break;
                    case 3:
                        trainTvTrainExpert.setText("一般");
                        starExpert = "3";
                        break;
                    case 4:
                        trainTvTrainExpert.setText("满意");
                        starExpert = "4";
                        break;
                    case 5:
                        trainTvTrainExpert.setText("非常满意");
                        starExpert = "5";
                        break;
                }
            }
        });

        // 培训内容打分
        trainEvaluateRatingBarContent.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float RatingCount) {
                switch ((int) RatingCount) {
                    case 0:
                        trainTvTrainContent.setText("请打分");
                        starContent = "0";
                        break;
                    case 1:
                        trainTvTrainContent.setText("非常不满意");
                        starContent = "1";
                        break;
                    case 2:
                        trainTvTrainContent.setText("不满意");
                        starContent = "2";
                        break;
                    case 3:
                        trainTvTrainContent.setText("一般");
                        starContent = "3";
                        break;
                    case 4:
                        trainTvTrainContent.setText("满意");
                        starContent = "4";
                        break;
                    case 5:
                        trainTvTrainContent.setText("非常满意");
                        starContent = "5";
                        break;
                }
            }
        });

        // 主办方
        trainEvaluateRatingBarSponsor.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float RatingCount) {
                switch ((int) RatingCount) {
                    case 0:
                        trainTvTrainSponsor.setText("请打分");
                        starSponsor = "0";
                        break;
                    case 1:
                        trainTvTrainSponsor.setText("非常不满意");
                        starSponsor = "1";
                        break;
                    case 2:
                        trainTvTrainSponsor.setText("不满意");
                        starSponsor = "2";
                        break;
                    case 3:
                        trainTvTrainSponsor.setText("一般");
                        starSponsor = "3";
                        break;
                    case 4:
                        trainTvTrainSponsor.setText("满意");
                        starSponsor = "4";
                        break;
                    case 5:
                        trainTvTrainSponsor.setText("非常满意");
                        starSponsor = "5";
                        break;
                }
            }
        });


        trainEvaluateBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });

        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }
        });

    }

    /**
     * 提交评价信息
     */
    private void submit() {

        if (starExpert.equals("0") || starContent.equals("0") || starSponsor.equals("0")) {
            showToast("请完善打分");
            return;
        }


        if (StringUtil.isStrEmpty(trainEvaluateEtOpinion.getText().toString())) {
            showToast("请填写建议");
            return;
        }


        // 提交评价
        BusinessEvaluate.evaluateForTrain(this, trainingId, trainEvaluateEtOpinion.getText().toString(), starExpert, starContent, starSponsor, mHandler);
    }


    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_EVALUATE_FOR_TRAIN_SUCCESS:
                        showToast("评价成功");
                        Intent intent = new Intent();
                        intent.putExtra("trainingId", trainingId);
                        setResult(RESULT_OK, intent);
                        finish();
                        break;
                    case MSG.MSG_EVALUATE_FOR_TRAIN_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_ALL_EVALUATE_LIST_SUCCESS:
                        setView(msg.obj.toString());
                        break;
                    case MSG.MSG_ALL_EVALUATE_LIST_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    /**
     * 设置视图view
     *
     * @param json
     */
    private void setView(String json) {

        NewEvaluateVO newEvaluateVO = FastJsonUtil.getBean(json, "body", "train", NewEvaluateVO.class);

        if (newEvaluateVO == null)
            return;

        trainEvaluateEtOpinion.setText(newEvaluateVO.getTrainSuggestion());
        trainTvTrainExpert.setVisibility(View.VISIBLE);
        trainTvTrainContent.setVisibility(View.VISIBLE);
        trainTvTrainSponsor.setVisibility(View.VISIBLE);
        setStar(Integer.parseInt(newEvaluateVO.getScoreForExpertTrain()), Integer.parseInt(newEvaluateVO.getScoreForTrainContent()), Integer.parseInt(newEvaluateVO.getScoreForSponsor()));
    }

    @Override
    public void callBack(Object object) {

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            back();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 返回
     */
    public void back() {
        Utils.finishActivity(TrainEvaluateActivity.this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }
}
